/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.projectdemo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.service.CrudService;
import com.foreveross.webbase.common.utils.StringUtils;
import com.foreveross.webbase.projectdemo.dao.ServerinformationDao;
import com.foreveross.webbase.projectdemo.entity.Projectapply;
import com.foreveross.webbase.projectdemo.entity.Serverinformation;
import com.foreveross.webbase.projectdemo.service.ProjectapplyService;
import com.foreveross.webbase.projectdemo.service.ServerinformationService;

/**
 * 审核管理Service
 * @author yangwanli
 * @version 2017-03-16
 */
@Service
public class ServerinformationServiceImpl extends CrudService<ServerinformationDao, Serverinformation> implements ServerinformationService {
	@Autowired
	private ProjectapplyService projectapplyService;
	
	/**
	 * 删除数据
	 * @param id
	 */
	@Transactional(readOnly = false)
	public void deleteId(String projectid) {
		dao.deleteId(projectid);
	}
	
	/**
	 * 查询分页数据(先查审核表在项目表)
	 * @param page 分页对象
	 * @param entity
	 * @return
	 */
	@Override
	public Page<Serverinformation> findPage(Page<Serverinformation> page, Serverinformation serverinformation) {
		serverinformation.setPage(page);
		page.setList(serverinformations(dao.findList(serverinformation)));
		return page;
	}

	/**
	 * 获取单条数据(根据申请表Id查询)
	 * @param projectId
	 * @return
	 */
	@Override
	public Serverinformation getProjectId(String projectId){
		Serverinformation serverinformation=dao.getProjectId(projectId);
		Projectapply py=projectapplyService.get(projectId);
		if(serverinformation!=null){
			serverinformation.setProjectname(py.getName());
			serverinformation.setStatus(py.getStatus());
			serverinformation.setTypeList(py.getTypeList());
			serverinformation.setApproval(py.getApproval());
		}
		return serverinformation;
	}

	
	/**
	 * 获取单条数据
	 * @param id
	 * @return
	 */
	@Override
	public Serverinformation get(String id) {
		return serverinformation(dao.get(id));
	}
	
	/**
	 * 获取单条数据
	 * @param entity
	 * @return
	 */
	@Override
	public Serverinformation get(Serverinformation entity) {
		return serverinformation(dao.get(entity));
	}
	
	/**
	 * 查询列表数据
	 * @param entity
	 * @return
	 */
	@Override
	public List<Serverinformation> findList(Serverinformation entity) {
		return serverinformations(dao.findList(entity));
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param entity
	 */
	@Override
	@Transactional(readOnly = false)
	public void save(Serverinformation entity) {
		if (entity.getIsNewRecord()){
			entity.preInsert();
			dao.insert(entity);
		}else{
			Projectapply projectapply= new Projectapply();
			projectapply.setId(entity.getProjectid());
			projectapply.setStatus(entity.getStatus());
			projectapply.setApproval(entity.getApproval());
			projectapplyService.saveUpdate(projectapply);
			entity.preUpdate();
			dao.update(entity);
		}
	}
	
	/**
	 * 获取Serverinformation实体对象集合
	 * @param serverinformations
	 * @return
	 */
	public List<Serverinformation> serverinformations(List<Serverinformation> serverinformations){
		for(Serverinformation sf : serverinformations){
			//跟据审核Entity的projectid在项目表查询项目信息，并将部分字段值付给审核Entity字段
			if(StringUtils.isNotEmpty(sf.getProjectid())){
				Projectapply projectapply=projectapplyService.get(sf.getProjectid());
				sf.setProjectname(projectapply.getName());
				sf.setStatus(projectapply.getStatus());
				sf.setTypeList(projectapply.getTypeList());
			}
		}
		return serverinformations;
	}
	
	/**
	 * 获取单个Serverinformation实体对象
	 * @param serverinformations
	 * @return
	 */
	public Serverinformation serverinformation(Serverinformation serverinformation){
		//跟据审核Entity的projectid在项目表查询项目信息，并将部分字段值付给审核Entity字段
		if(StringUtils.isNotEmpty(serverinformation.getProjectid())){
			Projectapply projectapply=projectapplyService.get(serverinformation.getProjectid());
			serverinformation.setProjectname(projectapply.getName());
			serverinformation.setStatus(projectapply.getStatus());
			serverinformation.setTypeList(projectapply.getTypeList());
		}
		return serverinformation;
	}
}